//入口文件
import Vue from 'vue'
import app from './App.vue'

//------------导入MintUI组件
// import { Header, Swipe, SwipeItem, Button ,Switch,Lazyload} from 'mint-ui'
// //导入MUI样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'
// //创建组件
// Vue.component(Header. name,Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.component(Switch.name, Switch);
// Vue.use(Lazyload)
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//------------导入路由包
import VueRouter from 'vue-router'
//安装路由
Vue.use(VueRouter)
//导入router.js
import router from './router.js'

//------------导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)

//-----------导入mockjs
import './mock.js'

//导入组件用于上下拉刷新
import VueScroller from 'vue-scroller'
Vue.use(VueScroller)

//导入Vuex
import Vuex from 'vuex'
Vue.use(Vuex);

//安装图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview);

//本地读取car
var car=JSON.parse(localStorage.getItem('car') || '[]');
var store=new Vuex.Store({
    state:{
        car:car,   //{id:,count,price,selected}
    },
    mutations:{
        //点击加入购物车保存信息到car中
        addToCar(state,goodsinfo){
        //如果已有商品，则修改商品个数count
        // 如果无商品，则goodsinfo添加到car中
            let flag=false
            state.car.some(i=>{
                if(i.id==goodsinfo.id){
                    i.count = parseInt(goodsinfo.count);
                    flag=true;
                    return true;
                }
            });
            if(!flag){
                state.car.push(goodsinfo);
            }
        //    每更新一次购物车，将数据存储到本地localStorage
            localStorage.setItem('car',JSON.stringify(state.car))

        },
        //购物车页面点击删除按钮，删除car中对应元素
        delShopCarItem(state,index){
            state.car.splice(index,1);
            localStorage.setItem('car',JSON.stringify(state.car))
        },
    //   通过购物车的按钮修改car中的count
        changeShopCarNum(state,data){
            state.car.forEach(i=>{
                if(i.id==parseInt(data.id)){
                    i.count=data.count
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        changeGoodsSelected(state,data){
            //将selected选中的状态赋值给car
            state.car.forEach((v,i)=>{
                if(data.id==v.id){
                    v.selected=data.selected;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
    },
    getters:{
        getCarCount(state){
            let allcount=0;
            state.car.forEach(i=>{
                allcount+=parseInt(i.count);
            })
            return allcount
        },
        getSelectedCount(state){
            let count=0;
            state.car.forEach(i=>{
                if(i.selected===true){
                    count+=parseInt(i.count);
                }
            })
            return count;
        },
        getSelectedPrice(state){
            let price=0;
            state.car.forEach(i=>{
                if(i.selected===true){
                    //因为price是￥1111的形式，所以先用substr返回新字符串，再转换成整数
                    price+=parseInt(i.price.substr(1))*parseInt(i.count)
                }
            })
            return price
        },

    },
});

var vm=new Vue({
    el: '#app',
    data: {},
    // 引入的Vue是runtime-only所以只能用render
    render: c => c(app),
    // components:{
    //     app
    // },
    router,
    store,

})

